package com.geekgo.flowcontroller.support.parse;

import com.geekgo.flowcontroller.support.ConfigurationContext;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * @author geekgo
 * @version 2016/8/5 11:58
 */
public class AnnotationDrivenBeanParser implements BeanDefinitionParser {

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        Class<ConfigurationContext> configurationContextClass = ConfigurationContext.class;
        RootBeanDefinition configContextBean = new RootBeanDefinition(configurationContextClass);

        String packages = element.getAttribute("packages");
        Preconditions.checkState(StringUtils.isNotEmpty(packages),"packages can't be empty");
        configContextBean.getPropertyValues().add("scanPackages",packages);
        parserContext.getRegistry().registerBeanDefinition(configurationContextClass.getName(),configContextBean);
        return configContextBean;
    }
}
